package com.shoesmail.filter;

import com.shoesmail.constants.RedisConstants;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @author ：CDB
 * @description：账号布隆过滤器
 * @date ：2025/9/16 22:09
 */
@Component
public class AccountBloomFilter {
    private static final String BLOOM_NAME = RedisConstants.BLOON_FILTER;
    private static final int EXPECTED_INSERTIONS = 10_000_000;
    private static final double FPP = 0.001;

    private final RBloomFilter<Long> bloomFilter;

    public AccountBloomFilter(RedissonClient redissonClient) {
        this.bloomFilter = redissonClient.getBloomFilter(BLOOM_NAME);
    }

    @PostConstruct
    public void init() {
        // 初始化过滤器（若不存在）
        if (!bloomFilter.isExists()) {
            bloomFilter.tryInit(EXPECTED_INSERTIONS, FPP);
        }
    }
    // 账号是否存在
    public boolean accountContain(long number) {
        return bloomFilter.contains(number);
    }
    // 放入布隆过滤器
    public void put(long number) {
        bloomFilter.add(number);
    }

}
